<template>
    <div class="components-container">
        <div class="layui-inline">
          <label class="layui-form-label" >逝者姓名</label>
            <div class="layui-input-inline">
              <el-input v-model="selectData.RegnameLike" placeholder="请输入逝者姓名" @keydown.enter.native="btnSearch"></el-input>
            </div>
        </div>
     
        <div class="layui-inline">
            <label class="layui-form-label" >变更日期</label>
            <el-date-picker v-date v-model="selectData.YhddateGreaterAndEquals" type="date" placeholder="选择日期范围" @change="btnSearch">
            </el-date-picker>
             <el-date-picker v-date v-model="selectData.YhddateLessAndEquals" type="date" placeholder="选择日期范围" @change="btnSearch">
            </el-date-picker>
        </div>
            <div class="layui-inline">
            <label class="layui-form-label">退款状态</label>
            <div class="layui-input-inline">
              <el-select v-model="YhdlogoEquals" placeholder="请选择" @change="btnSearch">
                <el-option label="全部" :value="null"></el-option>
                <el-option
                  v-for="item in options"
                  :key="item.Value"
                  :label="item.Text"
                  :value="item.Value">
                </el-option>
              </el-select>
            </div>
        </div>
        <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
        <el-button  @click="dataRepeat">重置</el-button>
        <div class="header-form">
          
        </div>
        <div style="margin-top:10px;">
            <ul class="toolList">
                <li><el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">刷新</el-button></li>
                <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
                  <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
                </li>
            </ul>
        </div>
        <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
        <depositAdd ref="depositAdd" @refreshData="getData"></depositAdd> 
         <printmodal ref="printPdf"></printmodal>
    </div>
</template>

<style  rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-button + .el-button {
  margin-left: 0;
}
.btnCenter {
  text-align: center;
}
</style>
<script>
import dataTable from "@/components/Table/DataTable";
import depositAdd from "./depositAdd";
import apiBase from "@/api/apiBase.js";
import { manage } from "@/mixins/managePage.js";
import { ORDER } from "@/const/orderConst.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import printmodal from "@/components/Pdf/Print";

const YES = "是";
const NO = "否";
export default {
  name: "deposit",
  mixins: [manage],
  data() {
    return {
      YhdlogoEquals: 'A',
      selectData: {
        RegnameLike: null,
        YhddateGreaterAndEquals: null,
        YhddateLessAndEquals: null,
      },
      options:[],
      dataLists: null,
    };
  },
  methods: {
    //获取表头
    getColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetDepositColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },
    //获取押金信息
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.YhddateLessAndEquals = formatDate(
        Data.YhddateLessAndEquals,
        "yyyyMMdd"
      );
      Data.YhddateGreaterAndEquals = formatDate(
        Data.YhddateGreaterAndEquals,
        "yyyyMMdd"
      );
      switch (this.YhdlogoEquals) {
        case 'A':
        Data.YhdTkMountEquals=0
          break;
        case 'B':
         Data.YhdTkMountGreater=0
          break;
        default:
          break;
      }
      let data = {
        PageIndex: this.pageOrder.currentPage,
        PageSize: this.pageOrder.pageSize,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data,
      };
      apiBase.DoService(data, apiBase.getApiAdress().Deposit.Query, res => {
        this.dataList = res.data.content.OutData;
        this.pageOrder.totalNum = res.data.content.RecordCount;
      });
    },
    //终结
    depEnd() {
      this.$confirm("确认是否终结?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            YHDID: this.currentRow.YHDID
          };
          apiBase.DoService(
            data,
            apiBase.getApiAdress().Deposit.DepositEnd,
            res => {
              this.$message({
                type: "success",
                message: "终结成功!"
              });
              this.getData();
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //取消终结
    depCalEnd() {
      this.$confirm("确认是否取消终结?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            YHDID: this.currentRow.YHDID
          };
          apiBase.DoService(
            data,
            apiBase.getApiAdress().Deposit.DepositCancelEnd,
            res => {
              this.$message({
                type: "success",
                message: "终结成功!"
              });
              this.getData();
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //功能操作
    operate(order) {
      this.order = order;
      // this.dataRepeat();
      switch (this.order) {
        case ORDER.ADD:
          this.$refs.depositAdd.open(ORDER.ADD);
          break;
        case ORDER.MODIFY:
          this.$refs.depositAdd.open(ORDER.MODIFY, this.currentRow.YHDID);
          break;
        case ORDER.REFUND:
          this.$refs.depositAdd.open(ORDER.REFUND, this.currentRow.YHDID);
          break;
        case ORDER.PRINTSLYJ:
          this.$refs.printPdf.open(ORDER.PRINTSLYJ, this.currentRow.YHDID);
          break;
        case ORDER.PRINTLCYJ:
          this.$refs.printPdf.open(ORDER.PRINTLCYJ, this.currentRow.YHDID);
          break;
        case PRINT.PRINTYH:
          this.$refs.printPdf.open(ORDER.PRINTYH, this.currentRow.YHDID);
          break;
        case ORDER.FINAL:
          this.depEnd();
          break;
        case ORDER.CALFINAL:
          this.depCalEnd();
          break;
        case ORDER.INFO:
          this.$refs.depositAdd.open(ORDER.INFO, this.currentRow.YHDID);
          break;
        case ORDER.CHARGE:
          this.$refs.depositAdd.open(ORDER.CHARGE,this.currentRow.YHDID);
          break;
        case '导出Excel':
          this.exportExcel();
          break;
        default:
      }
    },
    btnShow(btn) {
      let status = null;
      let balance = null;
      let YHLSOURCE = [];
      if (this.currentRow) {
        status = this.currentRow.YHDLOGO;
        balance = this.currentRow.YHDSYMOUNT;
        YHLSOURCE = this.currentRow.YHLSOURCE;
      }
      switch (btn) {
        case ORDER.ADD:
          return true;
          break;
        case ORDER.MODIFY:
          // return status === 'A' || status === 'C';
          return this.currentRow;
          break;
        case ORDER.PRINTSLYJ:   //打印守灵室押金清单
          return YHLSOURCE.includes("B")
          break;
        case ORDER.PRINTLCYJ:   //打印冷藏押金清单
          return YHLSOURCE.includes("C")
          break;    
        case ORDER.PRINTYH:   //打印延后结算单
          return YHLSOURCE.includes("D")
          break;
        case ORDER.REFUND:   //退款
          // return balance > 0 && status === 'C';
          return this.currentRow;
          break;
        case ORDER.CHARGE:   // 收费
          // return status === 'A' || status === 'C';
          return status === 'A';
          break;
        case ORDER.FINAL:
          return status === 'C';
          break;
        case ORDER.CALFINAL:
          return status === 'B';
          break;
        case ORDER.INFO:
          return this.currentRow;
          break;
        case '导出Excel':
          return true;
          break;
        default:
      }
    },
    //数据重置
    dataRepeat() {
      this.YhdlogoEquals= 'A',
      this.selectData = {}
      this.getData()
    },
    exportExcel() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.YhddateLessAndEquals = formatDate(
        Data.YhddateLessAndEquals,
        "yyyyMMdd"
      );
      Data.YhddateGreaterAndEquals = formatDate(
        Data.YhddateGreaterAndEquals,
        "yyyyMMdd"
      );
      let data = {
        PageIndex: -1,
        PageSize: this.pageOrder.pageSize,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data,
      };
      apiBase.DoService(data, apiBase.getApiAdress().Deposit.Query, res => {
        this.dataLists = res.data.content.OutData;
        var startTime;
      var endTime;
      if(this.selectData.YhddateGreaterAndEquals){
         startTime = this.selectData.YhddateGreaterAndEquals.toLocaleDateString();
      }else{
        startTime = '空';
      }
      if(this.selectData.YhddateLessAndEquals){
         endTime = this.selectData.YhddateLessAndEquals.toLocaleDateString();
      }else{
        endTime = '空';
      }
      let headername;
      let addrName;
      if(!this.selectData.YhdlogoEquals){
        addrName = '全部';
      }else{
        var copyarea = this.options;
        let addrId = this.selectData.YhdlogoEquals;
        copyarea.forEach(function(item, index, arr) {
            if (item.value == addrId) {
              addrName = item.label;
            }
        });
      }
      headername = '押金报表' + '__' + addrName + '(' + startTime + '-' + endTime + ')';
      
      if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      // this.downloadLoading = true;
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.dataLists, headername);
        // this.downloadLoading = false;
      });
      });
      
    },
  },
  components: {
    dataTable,
    depositAdd,
    printmodal,
  },
  created() {
          this.getColumns();
          this.getData();
          //获取退款状态下拉
          apiBase.DoService(
          {},
          apiBase.getApiAdress().Deposit.GetYhdRefund,
          res => {
              this.options = res.data.content;
          }
      );
  }
};
</script>

